package com.lh.controller;

import com.lh.entity.Advertisement;
import com.lh.service.AdvertisementService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: LuoHang
 * @Date: 2021/2/14 0:13
 * @Description
 */
@Controller
public class AdvertisementController {
    @Autowired
    private AdvertisementService advertisementService;

    //status:0 失效   1 有效

    /**
     * 后台管理之获取广告列表页面
     *
     * @return 视图模型
     */
    @RequestMapping("/advertisementListHtml")
    public ModelAndView advertisementListHtml() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/advertisement_list");
        return modelAndView;
    }

    /**
     * 广告列表之获取后台广告数据功能 lay_ui需要的json数据格式
     *
     * @param request 请求对象
     * @param page    当前页
     * @param limit   每页显示的条数
     * @return json格式数据
     */
    @RequestMapping("/getAdvertisementsList")
    @ResponseBody
    public Map<String, Object> getAdvertisementsList(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit) {
        String id = request.getParameter("id");
        String companyName = request.getParameter("companyName");
        String minMoney = request.getParameter("minMoney");
        String maxMoney = request.getParameter("maxMoney");
        String advertisementStatus = request.getParameter("advertisementStatus");
        Map<String, Object> query = new HashMap<>(5);
        query.put("limit", limit);
        query.put("beginSize", (page - 1) * limit);
        if (!StringUtils.isNullOrEmpty(id)) {
            query.put("id", Integer.valueOf(id));
        }
        if (!StringUtils.isNullOrEmpty(companyName)) {
            query.put("companyName", companyName);
        }
        if (!StringUtils.isNullOrEmpty(minMoney)) {
            query.put("minMoney", new BigDecimal(minMoney));
        }
        if (!StringUtils.isNullOrEmpty(maxMoney)) {
            query.put("maxMoney", maxMoney);
        }
        String test = "请选择";
        if (!StringUtils.isNullOrEmpty(advertisementStatus) && !test.equals(advertisementStatus)) {
            query.put("advertisementStatus", advertisementStatus);

        }
        List<Advertisement> advertisements = advertisementService.getAdvertisements(query);

        Map<String, Object> map = new HashMap<>(4);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", advertisements);
        map.put("count", advertisementService.getCount(query));
        return map;
    }

    @RequestMapping("/addAdvertisementHtml")
    public ModelAndView addAdvertisementHtml() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/advertisement_add");
        return modelAndView;
    }

    /**
     * 添加广告位
     *
     * @param companyName        公司名
     * @param advertisementMoney 广告费
     * @param gmtCreate          开始时间
     * @param gmtEnd             结束时间
     * @param advertisementType  广告类型
     * @param advertisementImg   图片
     * @return 是否添加成功
     */
    @RequestMapping("/addAdvertisement")
    @ResponseBody
    public Map<String, Object> addAdvertisement(@RequestParam String companyName, @RequestParam BigDecimal advertisementMoney,
                                                @RequestParam String gmtCreate, @RequestParam String gmtEnd,
                                                @RequestParam String advertisementType, @RequestParam String advertisementImg) {
        Advertisement advertisement = new Advertisement(null, companyName, advertisementMoney, new Timestamp(getTimeToLong(gmtCreate)), null, new Timestamp(getTimeToLong(gmtEnd)), 1, advertisementType, advertisementImg);
        int count = advertisementService.addAdvertisement(advertisement);
        Map<String, Object> res = new HashMap<>(1);
        res.put("data", count > 0 ? "添加成功" : "添加失败");
        return res;
    }

    @RequestMapping("/updateAdvertisementHtml")
    public ModelAndView updateAdvertisementHtml(@RequestParam Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("advertisementInfo", advertisementService.getAdvertisementById(id));
        modelAndView.setViewName("admin/advertisement_edit");
        return modelAndView;
    }

    /**
     * 更新信息
     *
     * @param request 请求对象
     * @param id      对应id
     * @return 是否成功
     */
    @RequestMapping("/updateAdvertisement")
    @ResponseBody
    public Map<String, Object> updateAdvertisement(HttpServletRequest request, @RequestParam Integer id) {
        String companyName = request.getParameter("companyName");
        String advertisementMoney = request.getParameter("advertisementMoney");
        String gmtCreate = request.getParameter("gmtCreate");
        String gmtEnd = request.getParameter("gmtEnd");
        String advertisementType = request.getParameter("advertisementType");
        String advertisementImg = request.getParameter("advertisementImg");
        Advertisement advertisement = advertisementService.getAdvertisementById(id);

        Map<String, Object> map = new HashMap<>(8);
        map.put("id", id);
        map.put("gmtModified", new Timestamp(System.currentTimeMillis()));
        if (!StringUtils.isNullOrEmpty(companyName) && !companyName.equals(advertisement.getCompanyName())) {
            map.put("companyName", companyName);
        }
        if (!StringUtils.isNullOrEmpty(advertisementMoney)) {
            BigDecimal money = BigDecimal.valueOf(Double.valueOf(advertisementMoney));
            if (!money.equals(advertisement.getAdvertisementMoney())) {
                map.put("advertisementMoney", money);
            }
        }
        if (!StringUtils.isNullOrEmpty(gmtCreate)) {
            Timestamp begin = new Timestamp(getTimeToLong(gmtCreate));
            if (!begin.equals(advertisement.getGmtCreate())) {
                map.put("gmtCreate", begin);
            }
        }
        if (!StringUtils.isNullOrEmpty(gmtEnd)) {
            Timestamp end = new Timestamp(getTimeToLong(gmtEnd));
            if (!end.equals(advertisement.getGmtEnd())) {
                map.put("gmtEnd", end);
            }
        }
        if (!StringUtils.isNullOrEmpty(advertisementType) && !advertisementType.equals(advertisement.getAdvertisementType())) {
            map.put("advertisementType", advertisementType);
        }
        if (!StringUtils.isNullOrEmpty(advertisementImg) && !advertisementImg.equals(advertisement.getAdvertisementImg())) {
            map.put("advertisementImg", advertisementImg);
        }
        int count = advertisementService.updateAdvertisement(map);

        Map<String, Object> res = new HashMap<>(1);
        res.put("data", count > 0 ? "修改成功" : "修改失败");
        return res;
    }

    @RequestMapping("/deleteAdvertisementById")
    @ResponseBody
    public Map<String, Object> deleteAdvertisementById(@RequestParam Integer id) {
        int count = advertisementService.deleteAdvertisementById(id);
        Map<String, Object> map = new HashMap<>(1);
        map.put("data", count > 0 ? "删除成功" : "删除失败");
        return map;
    }

    private Long getTimeToLong(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date != null) {
            return date.getTime();
        } else {
            return Long.valueOf("0");
        }
    }
}
